//
//  XYJLanguageTopView.m
//  徙游记-01
//
//  Created by hcios on 2017/7/17.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJLanguageTopView.h"

@implementation XYJLanguageTopView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}


-(void)setupSubViewsFromText:(NSString *)fromText toText:(NSString *)toText{
    _fromBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_fromBtn setFrame:RECT_MAKE(0, 0, (SCREEN_WIDTH - 80) / 2, 30)];
    [_fromBtn setTitle:fromText forState:UIControlStateNormal];
    _fromBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_fromBtn.titleLabel setFont:[UIFont fontWithName:FONT_NAME_LTH size:15]];
    [_fromBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_fromBtn addTarget:self action:@selector(clickFrom:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_fromBtn];
    
    
    _changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_changeBtn setFrame:RECT_MAKE((SCREEN_WIDTH - 80) / 2, 0, 80, 30)];
    [_changeBtn setBackgroundColor:[UIColor cyanColor]];
    [_changeBtn addTarget:self action:@selector(clickChange:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_changeBtn];
    
    
    _toBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_toBtn setFrame:RECT_MAKE((SCREEN_WIDTH - 80) / 2 + 80, 0, (SCREEN_WIDTH - 80) / 2, 30)];
    [_toBtn setTitle:toText forState:UIControlStateNormal];
    _toBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_toBtn.titleLabel setFont:[UIFont fontWithName:FONT_NAME_LTH size:15]];
    [_toBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_toBtn addTarget:self action:@selector(clickTo:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_toBtn];
}


-(void)clickFrom:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickFrom:)]) {
        [self.delegate clickFrom:sender];
    }
}

-(void)clickTo:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickTo:)]) {
        [self.delegate clickTo:sender];
    }
}

-(void)clickChange:(id)sender{
    //点击交换
    CGFloat fromX = 0;
    if (fromX == _fromBtn.frame.origin.x) {
        [UIView animateWithDuration:0.7 animations:^{
            [_toBtn setFrame:RECT_MAKE(0, 0, (SCREEN_WIDTH - 80)/2, 30)];
            [_fromBtn setFrame:RECT_MAKE(CGRectGetMaxX(_changeBtn.frame), 0, (SCREEN_WIDTH - 80)/2, 30)];
        }];
    }else{
        [UIView animateWithDuration:0.7 animations:^{
            [_fromBtn setFrame:RECT_MAKE(0, 0, (SCREEN_WIDTH - 75)/2, 30)];
            [_toBtn setFrame:RECT_MAKE(CGRectGetMaxX(_changeBtn.frame), 0, (SCREEN_WIDTH - 80)/2, 30)];
        }];
    }
}


@end
